"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能测试
Case Name   : 使用元命令\dC+查看自定义的转换关系，转换关系存在
Create At   : 2022/05/17
Owner       : @kyeleze
Description :
    1.创建自定义转化函数
    2.创建自定义转换关系
    3.通过元命令查看创建的转换关系
    4.清理环境
Expect      :
    1.转化函数创建成功
    2.自定义转换关系创建成功
    3.转换关系通过元命令查看成功
    4.清理环境成功
History     :
"""

import unittest
import os
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
logger = Logger()


class TestCreateCast0077(unittest.TestCase):

    def setUp(self):
        logger.info(f'--------{os.path.basename(__file__)} start--------')
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.function_name = 'f_create_cast_0077'
        self.func_cmd = f'''
        create or replace function {self.function_name}(in raw)
        returns integer
        as
        \$\$
        declare
        begin
        return 10;
        end;
        \$\$
        language plpgsql;                       
        '''
        self.cast_cmd = f'''drop cast if exists (raw as integer) cascade;
                            create cast(raw as integer) with function 
                            {self.function_name}(raw) as implicit;'''
        self.meta_command_cmd = f'''\dC+ raw*'''
        self.rm_cmd = f'''
        drop cast if exists (raw as integer) cascade;
        drop function if exists {self.function_name}(raw) cascade;'''

    def test_create_cast_0077(self):
        text = '--------step1:创建自定义转化函数   expect:函数创建成功--------'
        logger.info(text)
        logger.info(self.func_cmd)
        function_msg = self.sh_primysh.execut_db_sql(self.func_cmd)
        logger.info(function_msg)
        self.assertIn(self.constant.CREATE_FUNCTION_SUCCESS_MSG,
                      function_msg, '执行失败' + text)
        text = '--------step2: 创建自定义转换关系    expect: 创建成功--------'
        logger.info(text)
        logger.info(self.cast_cmd)
        cast_msg = self.sh_primysh.execut_db_sql(self.cast_cmd)
        logger.info(cast_msg)
        self.assertIn('CREATE CAST', cast_msg , '执行失败' + text)
        text = '--------step3:创建自定义转换关系后，通过元命令查看建立的转换关系   ' \
               'expect: 转换关系通过元命令查看成功--------'
        logger.info(text)
        logger.info(self.meta_command_cmd)
        meta_msg = self.sh_primysh.execut_db_sql(self.meta_command_cmd)
        logger.info(meta_msg)
        self.assertIn(self.function_name, meta_msg, '执行失败' + text)

    def tearDown(self):
        text = '--------step4:后置处理,清理环境   expect:清理成功--------'
        logger.info(text)
        logger.info(self.rm_cmd)
        rm_msg = self.sh_primysh.execut_db_sql(self.rm_cmd)
        logger.info(rm_msg)
        self.assertIn(self.constant.DROP_FUNCTION_SUCCESS_MSG, rm_msg,
                      '执行失败' + text)
        self.assertIn('DROP CAST', rm_msg, '执行失败' + text)
        logger.info(f'--------{os.path.basename(__file__)} end--------')
